#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package org.jeecg.smallTools;

import org.jeecg.common.util.DateUtils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * 判断用户创建时间在一星期范围内
 */
public class UserCreationTimeChecker {

    public static boolean isWithinAWeek(LocalDateTime userCreationTime) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneWeekAgo = now.minus(7, ChronoUnit.DAYS);

        return userCreationTime.isAfter(oneWeekAgo) && userCreationTime.isBefore(now);
    }

    public static LocalDateTime convertDateToLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }


    public static void main(String[] args) {
        // 假设用户创建时间为2022-03-01T12:00:00
        Date date = DateUtils.str2Date("2024-03-20 12:00:00", DateUtils.datetimeFormat.get());
        LocalDateTime userCreationTime = convertDateToLocalDateTime(date);

        if (isWithinAWeek(userCreationTime)) {
            System.out.println("用户创建时间在一星期范围内！");
        } else {
            System.err.println("用户创建时间不在一星期范围内！");
        }
    }
}
